<script setup lang="ts">
/**
 * 系统管理-系统数据-行政区划-编辑
 */
import {ref} from "vue";
import {ElMessage, type FormInstance} from "element-plus";
import {editNationAreaApi} from "@/assets/api/systemManagement/data/nationArea";
import type {NationArea} from "@/components/interface/data/nationArea";


const props = defineProps<{
  form: NationArea
}>()

const emits = defineEmits(['save', 'close'])

// 表单验证
const formRef = ref()
const rules = {
  code: [
    {required: true, message: '请输入行政区划编码', trigger: 'blur'},
  ],
  name: [
    {required: true, message: '请输入名称', trigger: 'blur'},
  ],
  parentId: [
    {required: true, message: '请输入父节点', trigger: 'blur'},
  ],
  type: [
    {required: true, message: '请选择级别', trigger: 'blur'}
  ],
  longitude: [
    {required: true, message: '请输入经度', trigger: 'blur'},
  ],
  latitude: [
    {required: true, message: '请输入纬度', trigger: 'blur'},
  ]
}

// 保存方法
const saveLoading = ref(false)
const save = async (formRef: FormInstance) => {
  if (!formRef) return
  await formRef.validate((valid: boolean) => {
    if (valid) {
      saveLoading.value = true
      editNationAreaApi(props.form).then(res => {
        saveLoading.value = false
        if (res.data.code === 200) {
          ElMessage.success({message: res.data.data, showClose: true})
          emits('save', true)
        } else {
          ElMessage.error({message: res.data.data, showClose: true})
        }
      })
    }
  })
}
</script>

<template>
  <div class="form-div">
    <el-form v-loading="saveLoading" :model="props.form" :rules="rules" ref="formRef" label-width="120px">
      <el-form-item label="ID">
        <el-input v-model="props.form.id" placeholder="自动生成" disabled></el-input>
      </el-form-item>
      <el-form-item label="行政区划编码" prop="code">
        <el-input type="number" v-model="props.form.code" placeholder="请输入行政区划编码"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="props.form.name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="父节点" prop="parentId">
        <el-input type="number" v-model="props.form.parentId" placeholder="请输入父节点"></el-input>
      </el-form-item>
      <el-form-item label="级别" prop="type">
        <el-select v-model="props.form.type" style="width: 200px" placeholder="请选择级别">
          <el-option label="省级" value="1"></el-option>
          <el-option label="市级" value="2"></el-option>
          <el-option label="区县" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input v-model="props.form.longitude" placeholder="请输入经度"></el-input>
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input v-model="props.form.latitude" placeholder="请输入纬度"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save(formRef)">保存</el-button>
        <el-button @click="emits('close')">关闭</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>

</style>
